package com.cdy.jxc_project.controller;

import com.cdy.jxc_project.domain.TGoods;
import com.cdy.jxc_project.domain.TPurchaseList;
import com.cdy.jxc_project.domain.TPurchaseListGoods;
import com.cdy.jxc_project.domain.TSupplier;
import com.cdy.jxc_project.domain.vo.InventoryQuantityVo;
import com.cdy.jxc_project.json.R;
import com.cdy.jxc_project.service.TGoodsService;
import com.cdy.jxc_project.service.TPurchaseListGoodsService;
import com.cdy.jxc_project.service.TPurchaseListService;
import com.cdy.jxc_project.service.TSupplierService;

import com.cdy.jxc_project.utils.AddSysLog;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/*
 * @Project:jxc_project
 * @Author:cdy(缘生)
 * @Motto:放下杂念,只为迎接明天更好的自己
 * @Date:2023/5/2 16:09
 * */
@RestController
@RequestMapping("/tPurchase")
public class TPurchaseController {

    @Resource
    private TPurchaseListService tPurchaseListService;

    @Resource
    private TPurchaseListGoodsService tPurchaseListGoodsService;

    @Resource
    private TSupplierService tSupplierService;

    @Resource
    private TGoodsService tGoodsService;

    //生成入库单号
    @PostMapping("/makeSheet")
    public R makeSheet(){
        String rkSheet= "JH" +System.currentTimeMillis()+new Random().nextInt(9999);
        List<TSupplier> list = tSupplierService.list();
        return R.ok().data("sheetCode",rkSheet).data("gys",list);
    }
    /*
    TPurchaseListService
     */

    @PostMapping("/insertTPurchaseList")
    public R insertTPurchaseList(@RequestBody TPurchaseList tPurchaseList){
        tPurchaseListService.insertTPurchaseList(tPurchaseList);
        Integer id = tPurchaseList.getId();
        return R.ok().data("purchaseListId",id);
    }

    @PostMapping("/queryTPurchaseList")
    public R queryTPurchaseList(@RequestBody TPurchaseList tPurchaseList){
        List<TPurchaseList> tPurchaseLists = tPurchaseListService.queryTPurchaseList(tPurchaseList);
        return R.ok().message("查询成功").data("tPurchaseLists",tPurchaseLists);
    }

    @PostMapping("/queryAll")
    public R queryAll(){
        List<TPurchaseList> tPurchaseLists = tPurchaseListService.queryAll();
        return R.ok().message("查询成功").data("tPurchaseLists",tPurchaseLists);
    }

    @PostMapping("/queryTPurchaseListGoodsByTPurchaseListId/{id}")
    public R queryTPurchaseListGoodsByTPurchaseListId(@PathVariable Integer id){
        List<TPurchaseListGoods> tPurchaseListGoods = tPurchaseListGoodsService.queryTPurchaseListGoodsByTPurchaseListId(id);
        return R.ok().message("查询成功").data("tPurchaseListGoods",tPurchaseListGoods);
    }
    /*
    TPurchaseListGoodsService
     */

    @PostMapping("/insertTPurchaseListGoods")
    public R insertTPurchaseListGoods(@RequestBody TPurchaseListGoods tPurchaseListGoods){
        tPurchaseListGoodsService.insertTPurchaseListGoods(tPurchaseListGoods);
        return R.ok().message("新增成功");
    }

    @PostMapping("/queryTPurchaseListGoods")
    public R queryTPurchaseListGoods(String code){
        List<TPurchaseListGoods> tPurchaseListGoods = tPurchaseListGoodsService.queryTPurchaseListGoods(code);
        return R.ok().message("查询成功").data("tPurchaseListGoods",tPurchaseListGoods);
    }

    @PostMapping("/queryAllTPurchaseListGoods")
    public R queryAllTPurchaseListGoods(){
        List<TPurchaseListGoods> tPurchaseListGoods = tPurchaseListGoodsService.queryAllTPurchaseListGoods();
        List<Integer> tPurchaseListGoodsNum = new ArrayList<>();
        List<String> tPurchaseListGoodsCode= new ArrayList<>();
        for (TPurchaseListGoods tPurchaseListGood : tPurchaseListGoods) {
           tPurchaseListGoodsCode.add(tPurchaseListGood.getCode());
           tPurchaseListGoodsNum.add(tPurchaseListGood.getNum());
        }
        return R.ok().message("查询成功").data("tPurchaseListGoodsNum",tPurchaseListGoodsNum).data("tPurchaseListGoodsCode",tPurchaseListGoodsCode);
    }

    //修改货物库存
    @PostMapping("/updateTGoodsInventoryQuantity")
    public R updateTGoodsInventoryQuantity(@RequestBody InventoryQuantityVo inventoryQuantityVo){
        tGoodsService.updateTGoodsInventoryQuantity(inventoryQuantityVo);
        return R.ok().message("库存修改成功");
    }
}
